/**
* 基础菜单 商品管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="searchForm" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchForm.goodsName" placeholder="输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="searchForm.goodsType" placeholder="输入商品类型"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
        <el-button size="small" type="primary" @click="dialog=true">批量新增</el-button>
        <el-button size="small" type="danger" @click="deleteByIds">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
              element-loading-text="拼命加载中" style="width: 100%;"
              @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="goodsName" label="商品名">
      </el-table-column>
      <el-table-column sortable prop="goodsType" label="商品类型">
      </el-table-column>
      <el-table-column sortable prop="goodsIntroduce" label="简介" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="goodsPrice" label="价格">
      </el-table-column>
      <el-table-column sortable prop="goodsStock" label="库存">
      </el-table-column>

      <el-table-column sortable prop="goodsImg" label="图片">
        <template slot-scope="scope">
          <el-image :src="aa+scope.row.goodsImg"></el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5,10,20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-form-item label="商品名" prop="deptName">
          <el-input size="small" v-model="editForm.goodsName" auto-complete="off"
                    placeholder="请输入商品名"></el-input>
        </el-form-item>

        <el-form-item label="商品类型" prop="deptNo">
          <el-input size="small" v-model="editForm.goodsType" auto-complete="off"
                    placeholder="请输入商品类型"></el-input>
        </el-form-item>

        <el-form-item label="简介" prop="deptNo">
          <el-input size="small" v-model="editForm.goodsIntroduce" type="textarea" auto-complete="off"
                    placeholder="请输入简介"></el-input>
        </el-form-item>


        <el-form-item label="价格" prop="deptNo">
          <el-input size="small" v-model="editForm.goodsPrice" auto-complete="off" placeholder="请输入价格"></el-input>
        </el-form-item>
        <el-form-item label="库存" prop="deptNo">
          <el-input size="small" v-model="editForm.goodsStock" auto-complete="off" placeholder="请输入库存"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="deptNo">
          <el-upload
            ref="upload"
            class="upload-demo"
            action=""
            :file-list="fileList"
            :auto-upload="false"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-change="onchange"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>


        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
    <!--批量新增-->
    <el-dialog
      title="批量新增商品"
      :visible.sync="dialog"
      width="auto"
      style="text-align: center"
    >
      <el-row v-for="(form, index) in formArr" :key="index">
        <el-form
          label-position="right"
          :model="form"
          :inline="true">
          <el-form-item>
            {{ index + 1 }}
          </el-form-item>
          <el-form-item label="商品名" prop="deptName">
            <el-input size="small" v-model="form.goodsName"
                      placeholder="请输入商品名"></el-input>
          </el-form-item>

          <el-form-item label="商品类型" prop="deptNo">
            <el-input size="small" v-model="form.goodsType" auto-complete="off"
                      placeholder="请输入商品类型"></el-input>
          </el-form-item>

          <el-form-item label="简介" prop="deptNo">
            <el-input size="small" v-model="form.goodsIntroduce" type="textarea" auto-complete="off"
                      placeholder="请输入简介"></el-input>
          </el-form-item>


          <el-form-item label="价格" prop="deptNo">
            <el-input size="small" v-model="form.goodsPrice" auto-complete="off" placeholder="请输入价格"></el-input>
          </el-form-item>
          <el-form-item label="库存" prop="deptNo">
            <el-input size="small" v-model="form.goodsStock" auto-complete="off" placeholder="请输入库存"></el-input>
          </el-form-item>


        </el-form>
      </el-row>
      <el-row justify="center">
        <el-button @click="addForm" plain type="primary">新增一行</el-button>
        <el-button @click="addList"    type="primary">批量新增</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import Request from "../../api/request";

export default {
  data() {
    return {
      dialog: false,
      multipleSelection:[],
      formArr: [],
      aa: 'data:image/png;base64,',
      fileList: [],
      currentPage: 1,
      size: 5,
      total: 0,
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {

        goodsName: '',
        goodsIntroduce: '',
        goodsType: '',
        goodsPrice: '',
        goodsStock: '',
        goodsImg: '',
      },
      searchForm: {
        goodsName: '',
        goodsType: '',
      },
      // 删除部门

      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数

    }
  },

  /**
   * 创建完毕
   */
  mounted() {
    this.select()
  },


  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 复选框选中后执行的方法
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteByIds(){
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
            Request.post("/goods/deletelist", this.multipleSelection).then(res => {
              if (res.data.data) {
                this.select();
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                this.select();
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

    },
    close() {
      this.dialog = false
      this.formArr = []
    },
    addForm() {
      this.formArr.push({
        goodsName: '',
        goodsIntroduce: '',
        goodsType: '',
        goodsPrice: '',
        goodsStock: '',
        goodsImg: '',
      })
    },
    addList() {
      Request.post("goods/addlist", this.formArr).then(res => {
        this.dialog = false
        this.loading = false
        if (res.data.data) {

          this.select();
          this.$message({
            type: 'success',
            message: '保存成功！'
          })

        } else {
          this.$message({
            type: 'info',
            message: res.data.message
          })
        }
      }).catch(err => {
          this.$message.error('保存失败，请稍后再试！')
        })
      this.formArr = []

    },
    onchange(file, fileList) {
      this.fileList = []

      this.fileList.push(file)
    },

    handleRemove(file, fileList) {
      this.fileList = []
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.select()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this.select()
    },
    select() {
      Request.get(
        "/goods?current=" + this.currentPage +
        "&size=" + this.size +
        "&goodsName=" + this.searchForm.goodsName +
        "&goodsType=" + this.searchForm.goodsType)
        .then(res => {
          this.loading = false

          this.listData = res.data.data.records
          // 分页赋值
          this.currentPage = res.data.data.current
          this.size = res.data.data.size
          this.total = res.data.data.total

        })
    },


    // 搜索事件
    search() {
      this.select();
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.goodsId = row.goodsId
        this.editForm.goodsName = row.goodsName
        this.editForm.goodsIntroduce = row.goodsIntroduce
        this.editForm.goodsType = row.goodsType
        this.editForm.goodsPrice = row.goodsPrice
        this.editForm.goodsStock = row.goodsStock
        this.editForm.goodsImg = row.goodsImg
        this.fileList = []
        this.fileList.push({url: this.aa + row.goodsImg})
      } else {
        this.title = '添加'
        this.editForm.goodsId = ''
        this.editForm.goodsName = ''
        this.editForm.goodsIntroduce = ''
        this.editForm.goodsType = ''
        this.editForm.goodsPrice = ''
        this.editForm.goodsStock = ''
        this.editForm.goodsImg = ''
        this.$refs.upload.clearFiles();
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {

          let formData = new FormData();
          formData.append('goods', JSON.stringify(this.editForm))

          if (this.fileList[0].raw != undefined)
            formData.append('multipartFile', this.fileList[0].raw)

          let res
          if (this.title == '添加') res = Request.post("goods", formData)
          else {
            if (this.fileList[0].raw != undefined) res = Request.put("goods", formData)
            else {

              res = Request.put("goods/edit", formData)

            }
          }
          res.then(res => {
            this.editFormVisible = false
            this.loading = false
            if (res.data.data) {
              this.select();
              this.$message({
                type: 'success',
                message: '保存成功！'
              })
            } else {
              this.$message({
                type: 'info',
                message: res.data.message
              })
            }
          })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        }
      })
    },
    // 删除公司
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          Request.delete("goods", {data: row})
            .then(res => {
              if (res.data.data) {
                this.select();
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                this.select();
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('公司删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>


